package com.barunsw.bwf.docu_server.board;

import java.io.File;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value = "/board")
public class BoardController {
	private final String BOARD_FILE_ROOT_PATH = "/Users/kimjs/Desktop/uploadFile";

	@Autowired
	BoardService boardService;

	@RequestMapping(value = "/list/{startPos}/{endPos}", method = RequestMethod.GET)
	public @ResponseBody Object[] getBoardList(
			@RequestBody @PathVariable int startPos,
			@RequestBody @PathVariable int endPos) throws Exception {
		
		int totalCount 			= boardService.getTotalCount();
		List<BoardVo> boardList = boardService.getBoardList(startPos, endPos);
		Object[] params			= new Object[2];
		params[0]				= totalCount;
		params[1]				= boardList;
		
		return params;
	}

	@RequestMapping(value="/searchList/{searchKey}/{search}/{dateOption}", method=RequestMethod.GET)
	public @ResponseBody List<BoardVo> getSearchList(
			@RequestBody @PathVariable String searchKey,
			@RequestBody @PathVariable String search,
			@RequestBody @PathVariable String dateOption) throws Exception {
		
		List<BoardVo> boardList = boardService.getSearchList(searchKey, search,dateOption);

		return boardList;
	}

	@RequestMapping(value="/readContent/{seq}", method=RequestMethod.GET)
	public @ResponseBody BoardVo getReadContent(@RequestBody @PathVariable String seq) throws Exception {
		BoardVo boardVo = boardService.getReadContent(seq);
		long redcount = this.updateReadCount(seq);

		return boardVo;
	}
	
	public int updateReadCount ( String seq ) throws Exception {
		return boardService.updateReadCount(seq);
	}

	@RequestMapping(value="/insertContent", method=RequestMethod.POST)
	@ResponseBody
	public String insertContent(BoardVo boardVo) throws Exception {

		List<MultipartFile> files = boardVo.getUploadfile();

		if ( files != null ) {
			StringBuffer fileNames = new StringBuffer();

			for ( MultipartFile multipartFile : files ) {
				String filename = multipartFile.getOriginalFilename();
				fileNames.append(filename + ";");

				try {
					File file = new File(BOARD_FILE_ROOT_PATH + File.separator + filename);
					multipartFile.transferTo(file);
				} 
				catch ( Exception ex ) {
					ex.printStackTrace();
				} 
			}
			
			boardVo.setFilename(fileNames.toString());
		}

		int result = boardService.insertContent(boardVo);

		return "OK";
	}

	@RequestMapping(value="/deleteContent/{seq}", method=RequestMethod.GET)
	public String deleteContent(@RequestBody @PathVariable String seq) throws Exception {
		int result = boardService.deleteContent(seq);

		return "redirect:/board/list";
	}

	@RequestMapping(value = "/attachFileDownload/{fileName}.{extension}", method = RequestMethod.GET)
	public ModelAndView attachFileDownload(
			@RequestBody @PathVariable String fileName, 
			@RequestBody @PathVariable String extension) throws Exception {
		
		String fullPath = BOARD_FILE_ROOT_PATH + File.separator + fileName + extension;
		File attachFile = new File(fullPath);

		return new ModelAndView("fileDownloadView", "attachFile", attachFile);
	}
}
